<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Areas;
use App\Http\Requests\AreasCreateRequest;
use  App\Http\Requests\AreasUpdateRequest;
class AreasController extends Controller
{
    protected $fields = [
        'areas_name' => '',
    ];
    
    public function index(Request $request){
      if($request->ajax()){
          $data = array();
          $data['draw'] = $request->get('draw');
          $start = $request->get('start');
          $length = $request->get('length');
          $order = $request->get('order');
          $columns = $request->get('columns');
          $search = $request->get('search');
          $data['recordsTotal'] = Areas::count();
          if (strlen($search['value']) > 0) {
              $data['recordsFiltered'] = Areas::where(function ($query) use ($search) {
                  $query->where('areas_name', 'LIKE', '%' . $search['value'] . '%');
              })->count();
          
              $data['data'] = Areas::where(function ($query) use ($search) {
                  $query->where('areas_name', 'LIKE', '%' . $search['value'] . '%');
              })
              ->skip($start)->take($length)
              ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
              ->get();
          
          }else{
              $data['recordsFiltered'] = Areas::count();
              $data['data'] = Areas::
              skip($start)->take($length)
              ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
              ->get();
          
          }
          
          return response()->json($data);
      }
     return view('admin.areas.index');
    }
    

    public function show($id){
    
    }
    
    public function create(){
        $data = [];
        foreach ($this->fields as $field => $default) {
            $data[$field] = old($field, $default);
        }
        return view('admin.areas.create', $data);
    }
    
  public function store(AreasCreateRequest $request)
    {

        $areas = new Areas();
        foreach (array_keys($this->fields) as $field) {
            $areas->$field = $request->get($field);
        }
        $areas->save();
        return redirect('/admin/areas')->withSuccess('添加成功！');
    }
    
   public function edit($id)
   {
       $areas = Areas::find((int)$id);
       if (!$areas) return redirect('/admin/permission')->withErrors("找不到该权限!");
       $data = ['id' => (int)$id];
       foreach (array_keys($this->fields) as $field) {
           $data[$field] = old($field, $areas->$field);
       }
       return view('admin.areas.edit', $data);
   }
   
   /**
    * Update the specified resource in storage.
    *
    * @param PermissionUpdateRequest|Request $request
    * @param  int $id
    * @return \Illuminate\Http\Response
    */
   public function update(AreasUpdateRequest $request, $id)
   {
       $areas = Areas::find((int)$id);
       foreach (array_keys($this->fields) as $field) {
           $areas->$field = $request->get($field);
       }
       $areas->save();
       return redirect('admin/areas')->withSuccess('修改成功！');
   }
   
   public function destroy($id){
       $role = Areas::find((int)$id);
       if ($role) {
           $role->delete();
       } else {
           return redirect()->back()
           ->withErrors("删除失败");
       }
   
       return redirect()->back()
       ->withSuccess("删除成功");
   }
    
}
